package com.scm.java.net.sup.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.scm.entity.base.DrugSns;
import com.scm.entity.base.SupDrugIn;
import com.scm.entity.base.SupDrugStock;
import com.scm.entity.base.Supplier;
import com.scm.entity.system.SysUsers;
import com.scm.java.net.sup.service.DrugInService;
import com.scm.java.net.system.action.BasicAction;
import com.scm.java.net.utils.DateUtils;
import com.scm.java.net.utils.StringUtils;

/**
 * 药品入库action
 * 
 * @author Administrator
 * 
 */
public class DrugInAction extends BasicAction implements ModelDriven<SupDrugIn>,Preparable {
	// 供应商列表
	private List<Supplier> supLists;
	private SupDrugIn drugIn;
	// 开始时间
	private Date beginDate;
	// 结束时间
	private Date endDate;
	private String[] sns;
	private DrugInService drugInService;

	// 显示入库单列表
	public String list() throws Exception {
		// 查找所有的供应商
		supLists = dao.queryList("supplier.getSupplier", null);
		if (endDate == null) {
			endDate = new Date();
		}
		if (beginDate == null) {
			beginDate = new Date(endDate.getTime() - 7 * 24 * 60 * 60 * 1000);
		}
		endDate = DateUtils.toDayEnd(endDate);
		beginDate = DateUtils.toDayStart(beginDate);
		paginatedQuery("drugin.getDrugins");
		return "list";
	}

	// 到达新增页面
	public String toAdd() throws SQLException {
		supLists = dao.queryList("supplier.getSupplier", null);
		drugIn.setProductionDate(new Date());
		return "addPage";
	}

	// 进行药品入库操作
	public void add() throws Exception {
		drugIn.setInvoiceid("MIS-"
				+ new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
		drugIn.setInputDate(new Date());

		String userId = ((SysUsers) session.get("loginUser")).getUserId();
		drugIn.setInputuserid(userId);
		List<DrugSns> snList = new ArrayList<DrugSns>();
		List<SupDrugStock> drugStockList = new ArrayList<SupDrugStock>();
		if (sns != null && sns.length > 0) {
			for (int i = 0; i < sns.length; i++) {
				// 串号表
				String[] sn = sns[i].split(",");
				DrugSns sns = new DrugSns();
				sns.setId(StringUtils.uniqueKey());
				sns.setInvoiceid(drugIn.getInvoiceid());
				sns.setSn1(sn[0]);
				sns.setSn2(sn[1]);
				snList.add(sns);
				// 库存表
				SupDrugStock drugStock = new SupDrugStock();
				drugStock.setSn1(sn[0]);
				drugStock.setSn2(sn[1]);
				drugStock.setStatus(0);
				BeanUtils.copyProperties(drugStock, drugIn);
				drugStockList.add(drugStock);
			}
		}
		drugInService.add(drugIn, snList, drugStockList, dao);
		PrintWriter out = response.getWriter();
		out.print("drugInManage_list.do");
	}

	public void dupSn() throws Exception {
		String sn1 = request.getParameter("sn1");
		String sn2 = request.getParameter("sn2");
		String method = request.getParameter("method");

		PrintWriter out = response.getWriter();

		Integer cnt = (Integer) dao.queryObject("drugStock.findDupSn1", sn1);
		if (cnt > 0) {
			out.print("dup");
			return;
		}

		if (method.equals("double")) {
			cnt = (Integer) dao.queryObject("drugStock.findDupSn2", sn2);
			if (cnt > 0) {
				out.print("dup");
				return;
			}
		}

		out.print("succ");

	}
    /**
     * 到达编辑入库单页面
     * @return
     * @throws SQLException
     */
	public String toEditDrugIn() throws SQLException {
		Map paraMap = new HashMap();
		paraMap.put("drugIn", drugIn);
		drugIn = (SupDrugIn) dao.queryList("drugin.getDrugins", paraMap).get(0);
		supLists = dao.queryList("supplier.getSupplier", null);
		return "editPage";
	}
    public void edit() throws Exception{
    	drugInService.edit(drugIn,sns,dao);
    	PrintWriter out = response.getWriter();
    	out.print("drugInManage_list.do");
    }
    /**
     * 删除入库单
     * @throws Exception
     */
    public String del() throws Exception{
    	drugInService.del(drugIn,dao);
    	return "succ";
    }
	public List<Supplier> getSupLists() {
		return supLists;
	}

	public void setSupLists(List<Supplier> supLists) {
		this.supLists = supLists;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	@Override
	public SupDrugIn getModel() {
		// TODO Auto-generated method stub
		return drugIn;
	}
	public SupDrugIn getDrugIn() {
		return drugIn;
	}

	public void setDrugIn(SupDrugIn drugIn) {
		this.drugIn = drugIn;
	}
	public DrugInService getDrugInService() {
		return drugInService;
	}

	public void setDrugInService(DrugInService drugInService) {
		this.drugInService = drugInService;
	}

	public String[] getSns() {
		return sns;
	}

	public void setSns(String[] sns) {
		this.sns = sns;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		drugIn=new SupDrugIn();
	}
}
